﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Windows.Forms;
using System.IO;

using SourceGrid;

namespace iPdf
{
    partial class SessionsGrid
    {
        internal class SessionOperationToolStrip : System.Windows.Forms.ToolStrip
        {
            #region Fields

            private System.Windows.Forms.ToolStripButton importToolStripButton;
            private System.Windows.Forms.ToolStripButton editToolStripButton;
            private System.Windows.Forms.ToolStripButton removeToolStripButton;
            private System.Windows.Forms.ToolStripDropDownButton sortToolStripDropDownButton;
            private System.Windows.Forms.ToolStripMenuItem sortByNameToolStripMenuItem;
            private System.Windows.Forms.ToolStripMenuItem sortByPageToolStripMenuItem;
            private System.Windows.Forms.ToolStripMenuItem sortByTimeModifiedToolStripMenuItem;
            private System.Windows.Forms.ToolStripButton invertRemoveToolStripButton;
            private System.Windows.Forms.ToolStripButton removeSessionsBeforeToolStripButton;
            private System.Windows.Forms.ToolStripButton removeSessionsAfterToolStripButton;
            private System.Windows.Forms.ToolStripButton removeAllSessionsToolStripButton;
            private System.Windows.Forms.CueToolStripTextBox searchToolStripTextBox;

            private CellContext cellContext;
            private int[] selectedRowsIndex;

            private static SessionsGridRowByNameComparer byNameComparer =
                new SessionsGridRowByNameComparer();
            private static SessionsGridRowByTimeModifiedComparer byTimeModifiedComparer =
                new SessionsGridRowByTimeModifiedComparer();
            private static SessionsGridRowByPageComparer byPageComparer =
                new SessionsGridRowByPageComparer();

            #endregion

            #region Properties
            protected SessionsGrid SessionsGrid
            {
                get
                {
                    return (SessionsGrid)cellContext.Grid;
                }
            }

            protected SourceGrid.Cells.Cell Cell
            {
                get
                {
                    return (SourceGrid.Cells.Cell)cellContext.Cell;
                }
            }

            protected TataSession CurrentSession
            {
                get
                {
                    return (TataSession)Cell.Tag;
                }
            }

            protected int CurrentRowIndex
            {
                get
                {
                    return cellContext.Position.Row;
                }
            }

            protected int RowsCount
            {
                get
                {
                    return SessionsGrid.RowsCount;
                }
            }

            public static IComparer DefaultComparer
            {
                get
                {
                    return byNameComparer;
                }
            }

            public CellContext CellContext
            {
                get
                {
                    return cellContext;
                }
                set
                {
                    cellContext = value;
                }
            }

            public event EventHandler Searching
            {
                add
                {
                    this.searchToolStripTextBox.TextBox.TextChanged += value;
                }
                remove
                {
                    this.searchToolStripTextBox.TextBox.TextChanged -= value;
                }
            }

            public string SearchText
            {
                get
                {
                    return this.searchToolStripTextBox.Text;
                }
            }
            #endregion

            #region Ctor
            public SessionOperationToolStrip()
            {
                InitializeComponent();
            }
            #endregion

            private void InitializeComponent()
            {
                this.importToolStripButton = new System.Windows.Forms.ToolStripButton();
                this.editToolStripButton = new System.Windows.Forms.ToolStripButton();
                this.sortToolStripDropDownButton = new System.Windows.Forms.ToolStripDropDownButton();
                this.sortByNameToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
                this.sortByTimeModifiedToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
                this.sortByPageToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
                this.removeToolStripButton = new System.Windows.Forms.ToolStripButton();
                this.invertRemoveToolStripButton = new System.Windows.Forms.ToolStripButton();
                this.removeSessionsBeforeToolStripButton = new System.Windows.Forms.ToolStripButton();
                this.removeSessionsAfterToolStripButton = new System.Windows.Forms.ToolStripButton();
                this.removeAllSessionsToolStripButton = new System.Windows.Forms.ToolStripButton();
                this.searchToolStripTextBox = new System.Windows.Forms.CueToolStripTextBox();

                this.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
                    this.importToolStripButton,
                    this.editToolStripButton,
                    this.sortToolStripDropDownButton,
                    this.removeToolStripButton,
                    this.invertRemoveToolStripButton,
                    this.removeSessionsBeforeToolStripButton,
                    this.removeSessionsAfterToolStripButton,
                    this.removeAllSessionsToolStripButton,
                    this.searchToolStripTextBox});

                // 
                // importToolStripButton
                // 
                this.importToolStripButton.Click += new EventHandler(ImportToolStripButton_Click);
                this.importToolStripButton.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image;
                this.importToolStripButton.Image = global::iPdf.Properties.Resources.ImportSession16;
                this.importToolStripButton.ImageTransparentColor = System.Drawing.Color.Magenta;
                this.importToolStripButton.Name = "importToolStripButton";
                this.importToolStripButton.Size = new System.Drawing.Size(23, 22);
                this.importToolStripButton.ToolTipText = "Import the selected session";
                // 
                // editToolStripButton
                // 
                this.editToolStripButton.Click += new EventHandler(EditToolStripButton_Click);
                this.editToolStripButton.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image;
                this.editToolStripButton.Image = global::iPdf.Properties.Resources.Edit16;
                this.editToolStripButton.ImageTransparentColor = System.Drawing.Color.Magenta;
                this.editToolStripButton.Name = "editToolStripButton";
                this.editToolStripButton.Size = new System.Drawing.Size(23, 22);
                this.editToolStripButton.ToolTipText = "Edit the selected session";
                // 
                // sortToolStripDropDownButton
                // 
                this.sortToolStripDropDownButton.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image;
                this.sortToolStripDropDownButton.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
                    this.sortByNameToolStripMenuItem,
                    this.sortByTimeModifiedToolStripMenuItem,
                    this.sortByPageToolStripMenuItem});
                this.sortToolStripDropDownButton.Image = global::iPdf.Properties.Resources.SortSessions;
                this.sortToolStripDropDownButton.ImageTransparentColor = System.Drawing.Color.Magenta;
                this.sortToolStripDropDownButton.Name = "sortToolStripDropDownButton";
                this.sortToolStripDropDownButton.Size = new System.Drawing.Size(29, 22);
                this.sortToolStripDropDownButton.ToolTipText = "Sort sessions";
                // 
                // sortByNameToolStripMenuItem
                // 
                this.sortByNameToolStripMenuItem.Checked = true;
                this.sortByNameToolStripMenuItem.Click += new EventHandler(SortMenuItem_Click);
                this.sortByNameToolStripMenuItem.CheckState = System.Windows.Forms.CheckState.Checked;
                this.sortByNameToolStripMenuItem.Name = "sortByNameToolStripMenuItem";
                this.sortByNameToolStripMenuItem.Size = new System.Drawing.Size(192, 22);
                this.sortByNameToolStripMenuItem.Tag = byNameComparer;
                this.sortByNameToolStripMenuItem.Text = "Sort by Name";
                // 
                // sortByTimeModifiedToolStripMenuItem
                // 
                this.sortByTimeModifiedToolStripMenuItem.Click += new EventHandler(SortMenuItem_Click);
                this.sortByTimeModifiedToolStripMenuItem.Name = "sortByTimeModifiedToolStripMenuItem";
                this.sortByTimeModifiedToolStripMenuItem.Size = new System.Drawing.Size(192, 22);
                this.sortByTimeModifiedToolStripMenuItem.Tag = byTimeModifiedComparer;
                this.sortByTimeModifiedToolStripMenuItem.Text = "Sort by Time Modified";
                // 
                // sortByPageToolStripMenuItem
                // 
                this.sortByPageToolStripMenuItem.Click += new EventHandler(SortMenuItem_Click);
                this.sortByPageToolStripMenuItem.Name = "sortByPageToolStripMenuItem";
                this.sortByPageToolStripMenuItem.Size = new System.Drawing.Size(192, 22);
                this.sortByPageToolStripMenuItem.Tag = byPageComparer;
                this.sortByPageToolStripMenuItem.Text = "Sort by Page";
                // 
                // removeToolStripButton
                // 
                this.removeToolStripButton.Click += new EventHandler(RemoveToolStripButton_Click);
                this.removeToolStripButton.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image;
                this.removeToolStripButton.Image = global::iPdf.Properties.Resources.Remove16;
                this.removeToolStripButton.ImageTransparentColor = System.Drawing.Color.Magenta;
                this.removeToolStripButton.Name = "removeToolStripButton";
                this.removeToolStripButton.Size = new System.Drawing.Size(23, 22);
                this.removeToolStripButton.Tag = new TataGridBase<TataSession>.RowJudge(RemoveJudge);
                this.removeToolStripButton.ToolTipText = "Remove selected sessions";
                // 
                // invertRemoveToolStripButton
                // 
                this.invertRemoveToolStripButton.Click += new EventHandler(RemoveToolStripButton_Click);
                this.invertRemoveToolStripButton.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image;
                this.invertRemoveToolStripButton.Image = global::iPdf.Properties.Resources.InvertRemove16;
                this.invertRemoveToolStripButton.ImageTransparentColor = System.Drawing.Color.Magenta;
                this.invertRemoveToolStripButton.Name = "invertRemoveToolStripButton";
                this.invertRemoveToolStripButton.Size = new System.Drawing.Size(23, 22);
                this.invertRemoveToolStripButton.Tag = new TataGridBase<TataSession>.RowJudge(InvertRemoveJudge);
                this.invertRemoveToolStripButton.ToolTipText = "Remove unslected sessions";
                // 
                // removeSessionsBeforeToolStripButton
                // 
                this.removeSessionsBeforeToolStripButton.Click += new EventHandler(RemoveToolStripButton_Click);
                this.removeSessionsBeforeToolStripButton.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image;
                this.removeSessionsBeforeToolStripButton.Image = global::iPdf.Properties.Resources.RemoveSessionsBefore;
                this.removeSessionsBeforeToolStripButton.ImageTransparentColor = System.Drawing.Color.Magenta;
                this.removeSessionsBeforeToolStripButton.Name = "removeSessionsBeforeToolStripButton";
                this.removeSessionsBeforeToolStripButton.Size = new System.Drawing.Size(23, 22);
                this.removeSessionsBeforeToolStripButton.Tag = new TataGridBase<TataSession>.RowJudge(RemoveSessionsBeforeJudge);
                this.removeSessionsBeforeToolStripButton.ToolTipText = "Remove sessions before the first selected session";
                // 
                // removeSessionsAfterToolStripButton
                // 
                this.removeSessionsAfterToolStripButton.Click += new EventHandler(RemoveToolStripButton_Click);
                this.removeSessionsAfterToolStripButton.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image;
                this.removeSessionsAfterToolStripButton.Image = global::iPdf.Properties.Resources.RemoveSessionsAfter;
                this.removeSessionsAfterToolStripButton.ImageTransparentColor = System.Drawing.Color.Magenta;
                this.removeSessionsAfterToolStripButton.Name = "removeSessionsAfterToolStripButton";
                this.removeSessionsAfterToolStripButton.Size = new System.Drawing.Size(23, 22);
                this.removeSessionsAfterToolStripButton.Tag = new TataGridBase<TataSession>.RowJudge(RemoveSessionsAfterJudge);
                this.removeSessionsAfterToolStripButton.ToolTipText = "Remove sessions after the last selected session";
                // 
                // removeAllSessionsToolStripButton
                // 
                this.removeAllSessionsToolStripButton.Click += new EventHandler(RemoveToolStripButton_Click);
                this.removeAllSessionsToolStripButton.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image;
                this.removeAllSessionsToolStripButton.Image = global::iPdf.Properties.Resources.RemoveAll16;
                this.removeAllSessionsToolStripButton.ImageTransparentColor = System.Drawing.Color.Magenta;
                this.removeAllSessionsToolStripButton.Name = "removeAllSessionsToolStripButton";
                this.removeAllSessionsToolStripButton.Size = new System.Drawing.Size(23, 22);
                this.removeAllSessionsToolStripButton.Tag = new TataGridBase<TataSession>.RowJudge(RemoveAllSessionsJudge);
                this.removeAllSessionsToolStripButton.ToolTipText = "Remove all sessions";
                //
                //searchToolStripTextBox
                //
                this.searchToolStripTextBox.CueText = "Search";
             }

            public void Reset()
            {
                sortByNameToolStripMenuItem.Checked = true;
                sortByPageToolStripMenuItem.Checked = false;
                sortByTimeModifiedToolStripMenuItem.Checked = false;

                this.searchToolStripTextBox.Clear();
            }

            public void UpdateToolStripButtons()
            {
                bool hasActiveCell = SessionsGrid.Selection.ActivePosition != Position.Empty;
                importToolStripButton.Enabled = hasActiveCell;
                editToolStripButton.Enabled = hasActiveCell;

                int[] selection = SessionsGrid.SelectedRowsIndex;
                bool selectionExists = selection.Length > 0;

                removeToolStripButton.Enabled = selectionExists;
                invertRemoveToolStripButton.Enabled = selectionExists;
                removeSessionsBeforeToolStripButton.Enabled = selectionExists;
                removeSessionsAfterToolStripButton.Enabled = selectionExists;
                
                if (selectionExists)
                {
                    int firstSelectedRow = selection[0];
                    removeSessionsBeforeToolStripButton.Enabled = firstSelectedRow > 1;

                    int lastSelectedRow = selection[selection.Length - 1];
                    removeSessionsAfterToolStripButton.Enabled = lastSelectedRow < RowsCount-1;
                }

                sortToolStripDropDownButton.Enabled = SessionsGrid.HasItems;
                removeAllSessionsToolStripButton.Enabled = SessionsGrid.HasItems;
                searchToolStripTextBox.Enabled = true;
            }

            private void RemoveRows(TataGridBase<TataSession>.RowJudge judge)
            {
                SessionsGrid.RemoveRows(judge, RemoveRow);
            }

            private void RemoveRow(int rowIndex)
            {
                if (rowIndex < 1 || rowIndex >= SessionsGrid.RowsCount)
                {
                    return;
                }

                SessionsGrid.RemoveRow(rowIndex);
            }

            private void ImportToolStripButton_Click(object sender, EventArgs e)
            {
                SessionsGrid.CommitImporting(CurrentSession);
            }

            private void EditToolStripButton_Click(object sender, EventArgs e)
            {
                cellContext.StartEdit();
            }

            private void SortMenuItem_Click(object sender, EventArgs e)
            {
                ToolStripMenuItem clickedMenuItem = (ToolStripMenuItem)sender;
                //clear the flags on other buttons and set the flag on the current button
                foreach (ToolStripMenuItem menuItem in sortToolStripDropDownButton.DropDown.Items)
                {
                    menuItem.Checked = clickedMenuItem == menuItem;
                }

                IComparer comparer = (IComparer)clickedMenuItem.Tag;

                SourceGrid.Cells.ColumnHeader sessionNameColHeader = (SourceGrid.Cells.ColumnHeader)
                    SessionsGrid.Id[0];

                bool ascend = true;
                if (sessionNameColHeader.SortComparer == comparer)
                {
                    switch (sessionNameColHeader.SortStyle)
                    {
                        case DevAge.Drawing.HeaderSortStyle.Ascending:
                            ascend = false;
                            break;

                        case DevAge.Drawing.HeaderSortStyle.Descending:
                        case DevAge.Drawing.HeaderSortStyle.None:
                            ascend = true;
                            break;
                    }
                }

                sessionNameColHeader.SortComparer = comparer;
                sessionNameColHeader.Sort(ascend);
            }

            private void RemoveToolStripButton_Click(object sender, EventArgs e)
            {
                ToolStripButton button = (ToolStripButton)sender;
                TataGridBase<TataSession>.RowJudge removeJudge = (TataGridBase<TataSession>.RowJudge)button.Tag;
                selectedRowsIndex = this.SessionsGrid.SelectedRowsIndex;
                RemoveRows(removeJudge);
                SessionsGrid.Selection.ResetSelection(false);
            }

            private bool RemoveJudge(int row, int removedRowsCnt, int rowsCnt)
            {
                return Array.IndexOf(selectedRowsIndex, row) != -1;
            }

            private bool InvertRemoveJudge(int row, int removedRowsCnt, int rowsCnt)
            {
                return Array.IndexOf(selectedRowsIndex, row) == -1;
            }

            private bool RemoveSessionsBeforeJudge(int row, int removedRowsCnt, int rowsCnt)
            {
                return row < CurrentRowIndex;
            }

            private bool RemoveSessionsAfterJudge(int row, int removedRowsCnt, int rowsCnt)
            {
                return row > CurrentRowIndex;
            }

            private bool RemoveAllSessionsJudge(int row, int removedRowsCnt, int rowsCnt)
            {
                return true;
            }

            public void SessionsGridSelection_SelectionChanged(object sender, SourceGrid.RangeRegionChangedEventArgs e)
            {
                SourceGrid.Selection.RowSelection rowSelection = (SourceGrid.Selection.RowSelection)sender;
                cellContext = new CellContext(rowSelection.Grid, rowSelection.ActivePosition);

                UpdateToolStripButtons();
            }

        }
    }
}
